﻿<%@ WebHandler Language="C#" Class="UploadLandscape" %>

using System;
using System.Web;

public class UploadLandscape : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Charset = "utf-8";

        HttpPostedFile file = context.Request.Files["Filedata"];

        if (file != null)
        {
            String aspxUrl = context.Request.Path.Substring(0, context.Request.Path.LastIndexOf("/") + 1);

            string path = "../file/temp/";

            string savePath = context.Server.MapPath(path);

            //文件保存目录URL
            String saveUrl = aspxUrl + path;

            string savename = System.Guid.NewGuid().ToString();
            string originName = savename + System.IO.Path.GetExtension(file.FileName);

            //保存原图
            file.SaveAs(System.IO.Path.Combine(savePath, originName));

            //返回图片链接
            context.Response.Write(string.Format("{{\"url\":\"{0}\"}}", saveUrl+originName));
        }
        else
        {
            context.Response.Write("错误");
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}